package com.yingxin.yxerp.module.activiti.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yx.framework.render.core.RenderApi;
import com.yingxin.yxerp.module.activiti.api.ProcessQueryService;
import com.yingxin.yxerp.module.activiti.biz.entity.InvalidateApply;
import com.yingxin.yxerp.module.activiti.biz.service.IInvalidateApplyService;
import com.yingxin.yxerp.module.activiti.biz.vo.req.InvalidateApplyCreateVo;
import com.yingxin.yxerp.module.activiti.biz.vo.req.InvalidateApplyQueryVo;
import com.yingxin.yxerp.module.activiti.biz.vo.resp.InvalidateApplyRespVo;
import com.yingxin.yxerp.module.attachment.service.IAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 作废申请 前端控制器
 * @author fanchao
 * @since 2022-11-15
 */

@Api(tags = "作废申请")
@RestController
@RequestMapping("/biz/invalidateApply")
public class InvalidateApplyController {

    @Autowired
    private IInvalidateApplyService invalidateApplyService;


    @Autowired
    private ProcessQueryService processQueryService;

    @Autowired
    private IAttachmentService attachmentService;

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<InvalidateApplyRespVo> page(@RequestBody InvalidateApplyQueryVo queryVo) {
        IPage<InvalidateApplyRespVo> page = invalidateApplyService.mapper().pageVo(PageHelper.in(queryVo), queryVo);
        processQueryService.collectTaskId(page.getRecords(), InvalidateApplyRespVo::getId, BusinessType.ZFSQ);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<InvalidateApplyRespVo> list(@RequestBody InvalidateApplyQueryVo queryVo) {
        return invalidateApplyService.mapper().listVo(queryVo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    @RenderApi
    public InvalidateApplyRespVo get(@PathVariable("id") String id) {
        InvalidateApplyRespVo apply = invalidateApplyService.mapper().getVo(id);
        apply.setActivitiInstance(processQueryService.getProcessByBusinessKey(apply.getId()));
        apply.setAttachmentList(attachmentService.listByBiz(id, BusinessType.ZFSQ));
        return apply;
    }


    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid InvalidateApplyCreateVo createVo) {
        InvalidateApply apply = new InvalidateApply();
        BeanUtil.copyProperties(createVo, apply);
        invalidateApplyService.create(apply);
        return apply.getId();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("billById/{id}")
    @RenderApi
    public InvalidateApplyRespVo getBillById(@PathVariable("id") String id) {
        InvalidateApplyRespVo apply = invalidateApplyService.mapper().getBillById(id);
        if (apply == null){
            throw new BizException("没有作废信息");
        }
        apply.setActivitiInstance(processQueryService.getProcessByBusinessKey(apply.getId()));
        apply.setAttachmentList(attachmentService.listByBiz(apply.getId(), BusinessType.ZFSQ));
        return apply;
    }
}
